package gov.pbc.nn.kjgl.item.view;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import gov.pbc.nn.core.bdf.service.UserService.DefaultUserRowMapper;
import gov.pbc.nn.core.dao.PbcnnCoreJdbcDao;
import gov.pbc.nn.core.utils.FileUtils;
import gov.pbc.nn.kjgl.item.dao.EmailItemDao;
import gov.pbc.nn.kjgl.item.entity.EmailItem;
import gov.pbc.nn.kjgl.item.template.entity.AbstractItem;
import gov.pbc.nn.kjgl.item.template.entity.ItemApproval;
import gov.pbc.nn.kjgl.item.template.service.AbstractItemService;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;

@Component
public class EmailApplyPR extends AbstractItemService {

	@Autowired
	private EmailItemDao emailItemDao;

	@Override
	public void saveItem(AbstractItem item)  throws Exception{
		EmailItem ei = (EmailItem) item;
		EntityState es = EntityUtils.getState(ei);
		if (es == EntityState.NEW) {
			ei.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
			ei.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
			this.emailItemDao.save(ei);
		} else {
			if (es == EntityState.MODIFIED) {
				this.emailItemDao.update(ei);
			}
		}
	}

	@DataProvider
	public EmailItem getEmailItem(String id) throws Exception {

		EmailItem emailItem = (EmailItem) this.emailItemDao.get(id, EmailItem.class);
		EmailItem emailNew = null;
		if (emailItem.getSignImg() != null) {
			emailNew = EntityUtils.toEntity(emailItem);
			EntityUtils.setValue(emailNew, "gaizhang", new String(emailItem.getSignImg()));
			return emailNew;
		} else {
			return emailItem;
		}

	}

	@Expose
	public String getImageByPath(String path) throws IOException {
		File f = new File(path);
		InputStream in = null;
		byte[] data = null;
		if (f.exists()) {
			try {
				in = new FileInputStream(path);
				data = new byte[in.available()];
				in.read(data);
				in.close();
				String encodeBase64String = Base64.encodeBase64String(data);
				String ss = "data:image/jpeg;base64," + encodeBase64String;
				return ss;
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Expose
	@Transactional
	public void saveImage(Map<String, Object> params) {
		String id = (String) params.get("id");
		String image = (String) params.get("image");
		EmailItem emailItem = (EmailItem) this.emailItemDao.get(id, EmailItem.class);
		emailItem.setSignImg(image.getBytes());
		this.emailItemDao.update(emailItem);
	}

	@DataProvider
	public List<IUser> getUserByDeptId() {
		String deptId = ContextHolder.getLoginUser().getDepts().get(0).getId();
		List<IUser> users = this.getUserByDept(deptId);
		return users;
	}

	public List<IUser> getUserByDept(String deptId) {
		String sql = "SELECT U.* FROM T_USER U INNER JOIN bdf2_user_dept ud ON U.USERNAME_ = ud.USERNAME_ WHERE ud.DEPT_ID_ = ?";
		PbcnnCoreJdbcDao jdbc = ContextHolder.getBean(PbcnnCoreJdbcDao.BEAN_ID);
		List<IUser> users = jdbc.getJdbcTemplate().query(sql, new Object[] { deptId }, new DefaultUserRowMapper());
		return users;
	}

}
